package com.keyue.controller;


import com.keyue.common.annotation.AnonymousAccess;
import com.keyue.common.constant.ServerResult;
import com.keyue.domain.vo.admin.AdminAuthRoleVO;
import com.keyue.domain.vo.admin.AdminRoleQueryVO;
import com.keyue.domain.vo.admin.AdminRoleVO;
import com.keyue.log.Log;
import com.keyue.service.admin.AdminRoleService;
import com.keyue.service.admin.AdminUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @date 2018-12-03
 */
@Api(tags = "系统：角色管理")
@RestController
@RequestMapping("/keyue/admin/roles")
public class RoleController {

    @Resource
    private AdminRoleService roleService;

    @Resource
    private AdminUserService userService;

    private static final String ENTITY_NAME = "role";


    @ApiOperation("获取单个role")
    @PostMapping(value = "/detail")
    @AnonymousAccess
    public ServerResult detail(@RequestBody @Validated AdminRoleQueryVO queryVO) {
        return ServerResult.success(roleService.getById(queryVO.getId()));
    }

    @ApiOperation("返回全部的角色")
    @PostMapping(value = "/all")
    @AnonymousAccess
    public ServerResult getAll(@RequestBody @Validated AdminRoleQueryVO queryVO) {
        return roleService.listByLevel(queryVO.getLevel());
    }

    @Log("查询角色")
    @ApiOperation("查询角色")
    @PostMapping(value = "/list_by_page")
    @AnonymousAccess
    public ServerResult listByPage(@RequestBody @Validated AdminRoleQueryVO queryVO) {
        return roleService.listByPage(queryVO);
    }

    @Log("新增角色")
    @ApiOperation("新增角色")
    @PostMapping(value = "/add")
    @AnonymousAccess
    public ServerResult add(@RequestBody @Validated AdminRoleVO roleVO) {
        return roleService.add(roleVO);
    }

    @Log("修改角色")
    @ApiOperation("修改角色")
    @PostMapping(value = "/edit")
    @AnonymousAccess
    public ServerResult edit(@RequestBody @Validated AdminRoleVO roleVO) {
        return roleService.edit(roleVO);
    }

    @Log("授权角色菜单")
    @ApiOperation("授权角色菜单")
    @PostMapping(value = "/auth_menu")
    @AnonymousAccess
    public ServerResult authMenu(@RequestBody @Validated AdminAuthRoleVO roleVO) {
        return roleService.authMenu(roleVO);
    }

    @Log("删除角色")
    @ApiOperation("删除角色")
    @PostMapping(value = "/delete")
    @AnonymousAccess
    public ServerResult delete(@RequestBody @Validated AdminRoleQueryVO queryVO) {
        return roleService.deleteById(queryVO);
    }
}
